package vn.edu.saigontech.ncms.web.bookstore.report;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import vn.edu.saigontech.ncms.model.bookstore.Item;
import vn.edu.saigontech.ncms.model.bookstore.OrderDetail;
import vn.edu.saigontech.ncms.service.ImportReceiptDetailService;
import vn.edu.saigontech.ncms.service.OrderDetailService;
import vn.edu.saigontech.ncms.service.ReturnDetailService;

public class ImportExportDetail {
	private Item item;
	private int firstRemain;
	private int sumOfImport;
	private int sumOfReturn; /* halnt */
	private int lastRemain;
	private Map<Double, Integer> priceMap;
	
	public ImportExportDetail(Item item, Date fromDate, Date toDate) {
		this.item = item;
		this.priceMap = new HashMap<Double, Integer>();
		init(fromDate, toDate);		
	}

	private void init(Date fromDate, Date toDate) {
		int firstSumOfOrder = OrderDetailService.INSTANCE.sumOfOrder(item, fromDate, false);
		int firstSumOfReturn = ReturnDetailService.INSTANCE.sumOfReturn(item, fromDate, false); /* halnt */
		int firstSumOfImport = ImportReceiptDetailService.INSTANCE.sumOfImport(item, fromDate, false);
		this.firstRemain = firstSumOfImport - firstSumOfOrder - firstSumOfReturn; /* halnt */
		
		this.sumOfImport = ImportReceiptDetailService.INSTANCE.sumOfImport(item, fromDate, toDate);
		
		int lastSumOfOrder = OrderDetailService.INSTANCE.sumOfOrder(item, toDate, true);
		int lastSumOfReturn = ReturnDetailService.INSTANCE.sumOfReturn(item, toDate, true); /* halnt */
		int lastSumOfImport = ImportReceiptDetailService.INSTANCE.sumOfImport(item, toDate, true);
		sumOfReturn = ReturnDetailService.INSTANCE.sumOfReturnByDate(item, fromDate, toDate); /* halnt */
		this.lastRemain = lastSumOfImport - lastSumOfOrder - lastSumOfReturn; /* halnt */
		
		List<OrderDetail> details = OrderDetailService.INSTANCE.findByDate(item, fromDate, toDate);
		for(OrderDetail detail : details) {
			double key = detail.getPriceDetail().getAmount();
			if(this.priceMap.containsKey(key)) {
				Integer value = this.priceMap.get(key);
				this.priceMap.put(key, value + detail.getQuantity());
			} else {
				this.priceMap.put(key, detail.getQuantity());
			}
		}
		if(details.isEmpty()) {
			this.priceMap.put(item.getPrice().getAmount(), 0);
		}
	}

	public Item getItem() {
		return item;
	}

	public void setItem(Item item) {
		this.item = item;
	}

	public int getFirstRemain() {
		return firstRemain;
	}

	public void setFirstRemain(int firstRemain) {
		this.firstRemain = firstRemain;
	}

	public int getSumOfImport() {
		return sumOfImport;
	}

	public void setSumOfImport(int sumOfImport) {
		this.sumOfImport = sumOfImport;
	}

	public int getLastRemain() {
		return lastRemain;
	}

	public void setLastRemain(int lastRemain) {
		this.lastRemain = lastRemain;
	}

	public Map<Double, Integer> getPriceMap() {
		return priceMap;
	}

	public void setPriceMap(Map<Double, Integer> priceMap) {
		this.priceMap = priceMap;
	}

	public int getSumOfReturn() {
		return sumOfReturn;
	}

	public void setSumOfReturn(int sumOfReturn) {
		this.sumOfReturn = sumOfReturn;
	}
}